import {
	preOrderApi,
	orderCreate,
	wechatOrderPay
} from '@/api/order.js';
import util from 'utils/util'

/**
 * 去商品详情
 */
export function goShopDetail(item, uid) {
	return new Promise(resolve => {
		if (item.activityH5 && item.activityH5.type === "1") {
			uni.navigateTo({
				url: `/pages/activity/goods_seckill_details/index?id=${item.activityH5.id}`
			})
		} else if (item.activityH5 && item.activityH5.type === "2") {
			uni.navigateTo({
				url: `/pages/activity/goods_bargain_details/index?id=${item.activityH5.id}&startBargainUid=${uid}`
			})
		} else if (item.activityH5 && item.activityH5.type === "3") {
			uni.navigateTo({
				url: `/pages/activity/goods_combination_details/index?id=${item.activityH5.id}`
			})
		} else {
			resolve(item);
		}
	});
}

/**
 * 活动商品、普通商品、购物车、再次购买预下单
 */
export function getPreOrder(preOrderType, orderDetails) {
	return new Promise((resolve, reject) => {
		preOrderApi({
			"preOrderType": preOrderType,
			"orderDetails": orderDetails
		}).then(res => {
			uni.navigateTo({
				url: '/pages/users/order_confirm/index?preOrderNo=' + res.data.preOrderNo
			});
		}).catch(err => {
			return util.Tips({
				title: err
			});
		})
	});
}

/**
 * 直接购买
 */
export function directBuy(preOrderType, orderDetails, payType = 'weixin', payChannel = 'routine') {
  return new Promise((resolve, reject) => {
    preOrderApi({
      "preOrderType": preOrderType,
      "orderDetails": orderDetails
    }).then(res => {
      const preOrderNo = res.data.preOrderNo;
      orderCreate({
        preOrderNo: preOrderNo,
        payType: payType,
        from: 'routine',
        shippingType: "1",
		addressId: "1",
        useIntegral: 0,
        couponId: 0,
        mark: "购买年度会员",
        payChannel: payChannel
      }).then(res => {
        const orderNo = res.data.orderNo;
        wechatOrderPay({
          orderNo: orderNo,
          payChannel: payChannel,
          payType: payType
        }).then(res => {
          const jsConfig = res.data.jsConfig;
          uni.requestPayment({
            timeStamp: jsConfig.timeStamp,
            nonceStr: jsConfig.nonceStr,
            package: jsConfig.packages,
            signType: jsConfig.signType,
            paySign: jsConfig.paySign,
            success: function(res) {
              resolve(res);
            },
            fail: function(err) {
              reject(err);
            }
          });
        }).catch(err => {
          reject(err);
        });
      }).catch(err => {
        reject(err);
      });
    }).catch(err => {
      reject(err);
    });
  });
}
